package autoorder.zsxmlv.service;

import common.zsxmlv.utils.HttpUtils;
import net.sf.json.JSONObject;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import work.weixin.qq.vo.text.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: ZSXM
 * @Description:
 * @Company: DingZheng
 * @Date: Create by 2018/6/4 12:14
 * @Version: 1.0
 * @Modify by:
 */
//@Component
public class SeleniumServiceDemo {

    public static void selenium(){
        ChromeDriver chromeDriver = new ChromeDriver();
        try {
            chromeDriver.get("http://buy.ccb.com/products/pd_0002167550.jhtml");
            chromeDriver.manage().window().maximize();
            Thread.sleep(3000);
            chromeDriver.findElementById("buy_now_button").click();
            Thread.sleep(1000);
            chromeDriver.switchTo().frame("jbox-iframe");
            Thread.sleep(1000);
            chromeDriver.findElementById("custname").sendKeys("fenghuaning");
            chromeDriver.findElementById("custpwd").sendKeys("1234560");
            Thread.sleep(1000);
            chromeDriver.findElementByName("sub").click();
            Thread.sleep(5000);
            chromeDriver.findElementByClassName("submit_btn").click();
            Thread.sleep(5000);
            WebElement qrcode2 = chromeDriver.findElementById("qrcode2");
            Thread.sleep(2000);
            String src = qrcode2.getAttribute("src");
            System.out.println(src);
            System.out.println("获取完成，关闭浏览器");
            chromeDriver.quit();
        } catch (Exception e) {
            System.out.println("==========================");
            System.out.println(e.getMessage());
            System.out.println("==========================");
//            e.printStackTrace();
            chromeDriver.quit();
            System.out.println("哈哈哈 错了");
//            e.printStackTrace();
        }
    }

    public static String corpid = "ww6d4fa42efc7d4377";
    public static String corpsecret = "pHaM7EyqX2qq9CJ5HaIfvaBFFxzH3D2VM1Dd4rm6cs8";

    public static String access_token = "zOZh5w65lkbrnyhk6pdha2-w27PGFS5kGz2u7ObzLFu2nqRh6WRNRK65RkvEFmRq4i3oB39ipvXbzvaxmARJ1caEfBhHWgIV8zWS1RDvO6WYjwVAXJoCoEiCrWGQ5Ki-ETy--rngDyhaeDfifLMEg6KY6pKAfGUXkG5xK1aTgIMG0pke2sFZV3qEZxRhXHAHh6FJzJAC7GCffGhQt2Njrg";
    public static String APP_ACCESS_TOKEN = "KltiXCuGUQREdml-9bdmsyDN8wo-Ac855EbEH_CdtwQaT3pDmINIFMAefzzyhraQBVrNN5cynK9dV92mQXmvgIyQoouQY_JTBhpN5Xv6rS7oU1PLJlCo2jhrjsWRCVrjWs2lI82ZpB3JXJnuBBlFEWTRTb-4xdw6kxqxyublzeFfeLftyZOnUMg2FQv6b1N-ZylIEiYs8SdpH-iwS5gxmw";

//    /**
//     * 发送应用消息
//     * @throws Exception
//     */
//    public static void sendMessage() throws Exception {
//        String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + APP_ACCESS_TOKEN;
//
////        HashMap<String, String> map = new HashMap<>();
//////        HashMap<String, String> map2 = new HashMap<>();
////        map.put("touser", "@all");
////        map.put("msgtype", "text");
////        map.put("agentid", "1000002");
////        map.put("toparty", "1");
////        map.put("totag", "");
////        map.put("text", "\"content\" : \"你的快递已到，请携带工卡前往邮件中心领取。\\n出发前可查看<a href=\\\"http://work.weixin.qq.com\\\">邮件中心视频实况</a>，聪明避开排队。\"");
////        map.put("content", "你的快递已到，请携带工卡前往邮件中心领取。\\n出发前可查看<a href=\\\"http://work.weixin.qq.com\\\">邮件中心视频实况</a>，聪明避开排队。");
//        TextMessage textMessage = new TextMessage();
//        textMessage.setAgentid("1000002");
//        textMessage.setTouser("ZhangSi");
//        textMessage.setToparty("1");
//        textMessage.setMsgtype("text");
//        Text text = new Text();
////        text.setContent("你的快递已到，请携带工卡前往邮件中心领取。出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>，聪明避开排队。");
//        text.setContent("你的快递已到，请携带工卡前往邮件中心领取。出发前可查看<img src=\"https://epay.ccb.com/epay/QrcodeServlet?width=150&height=150&qrcode=9980005726480723660821\" width=\"100\" height=\"100\" />，聪明避开排队。");
//        textMessage.setText(text);
//
//        JSONObject jsonObject = JSONObject.fromObject(textMessage);
//        System.out.println(jsonObject.toString());
//
//        String post = HttpUtils.post(url, jsonObject.toString());
//        System.out.println(post);
//
//    }

    /**
     * 获取标签列表
     * @throws Exception
     */
    public static void getTagByList() throws Exception {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list?access_token=" + APP_ACCESS_TOKEN;
        String s = HttpUtils.get(url);
        System.out.println(s);
    }

    /**
     * 获取accessToken
     * @throws Exception
     */
    public static String getAccessToken() throws Exception {
//        String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret;
        String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=cIH775_GxyaSz1GeGBUqWa9uEzpivAOgcMtlsFzK5P0";
        String token = HttpUtils.get(url);
        System.out.println(token);
        return token;
    }

    public static String getAppAccessToken(){
//        String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/set?access_token="+access_token;
        return "";
    }

    /**
     * 获取所有用户
     * @throws Exception
     */
    public static void getUserlist() throws Exception {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=" + APP_ACCESS_TOKEN +
                "&department_id=1&fetch_child=1";
        String s = HttpUtils.get(url);
        System.out.println(s);
    }

//    创建群聊会话
    public static void createChat() throws Exception {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/appchat/create?access_token=" + APP_ACCESS_TOKEN;
        // 群聊名
        String name = "这是一个有趣的名字";
        // 指定群主的id
        String owner = "ZhangSi";
        // 群成员id列表。至少2人，至多500人
        List userlist = new ArrayList();
        // 群聊的唯一标志，不能与已有的群重复；字符串类型，最长32个字符。只允许字符0-9及字母a-zA-Z
        String chatid = "123456789";

        userlist.add("ZhangSi");
        userlist.add("ShangKai");
//        userlist.add("XiaoXiaoDeRenA");

        String body = "{\n" +
                "    \"name\" : \"这是一个有趣的名字\",\n" +
                "    \"owner\" : \"ZhangSi\",\n" +
                "    \"userlist\" : [\"ZhangSi\", \"stars\", \"XiaoXiaoDeRenA\"],\n" +
                "    \"chatid\" : \"123456789\"\n" +
                "}";

        HashMap<String, String> map = new HashMap<>();
        map.put("name", name);
        map.put(owner, owner);
        map.put("userlist", userlist.toString());
        map.put("chatid", chatid);
        System.out.println(map.toString());


        StringBuffer sb = new StringBuffer();
//        String post = HttpUtils.post(url, map);
        String post = HttpUtils.post(url, body);
        System.out.println(post);
    }

    public static void getAppList() throws Exception {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/list?access_token="+ SeleniumServiceDemo.access_token;
        String s = HttpUtils.get(url);
        System.out.println(s);

    }

    public static void getApplication() throws Exception {
        String accessToken = "zOZh5w65lkbrnyhk6pdha2-w27PGFS5kGz2u7ObzLFu2nqRh6WRNRK65RkvEFmRq4i3oB39ipvXbzvaxmARJ1caEfBhHWgIV8zWS1RDvO6WYjwVAXJoCoEiCrWGQ5Ki-ETy--rngDyhaeDfifLMEg6KY6pKAfGUXkG5xK1aTgIMG0pke2sFZV3qEZxRhXHAHh6FJzJAC7GCffGhQt2Njrg";
        String agentid = "1000002";
        String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/get?access_token=ACCESS_TOKEN&agentid=AGENTID";
        url = url.replaceAll("ACCESS_TOKEN", APP_ACCESS_TOKEN).replaceAll("AGENTID", agentid);
        String s = HttpUtils.get(url);
        System.out.println(s);
    }

    /**
     * 获取部门列表
     * @throws Exception
     */
    public static void getDepotList() throws Exception {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token="+APP_ACCESS_TOKEN+"&&id=";
        String s = HttpUtils.get(url);
        System.out.println(s);
    }


    public static void main(String[] args) {
//        SeleniumService.selenium();
        try {
            SeleniumServiceDemo.getAccessToken();
//            SeleniumServiceDemo.sendMessage();
//            SeleniumService.createChat();
//            SeleniumService.getAccessToken();
//            SeleniumServiceDemo.sendMessage();
//            SeleniumService.getTagByList();
//            SeleniumService.getUserlist();
//            SeleniumService.getDepotList();
//            SeleniumService.getApplication();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
